package com.iscas.tech.controller;

import com.iscas.tech.entity.AtBaseuser;
import com.iscas.tech.param.AtBaseuserPageParam;
import com.iscas.tech.service.AtBaseuserService;
import com.iscas.tech.vo.AtBaseuserQueryVo;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.LoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 前端用户注册表 控制器
 *
 * @author yangyi
 * @since 2020-10-26
 */
@Slf4j
@RestController
@RequestMapping("/clientUser")
@Api(value = "前端用户注册表API", tags = {"前端用户注册表"})
public class AtBaseuserController extends BaseController {

    @Autowired
    private AtBaseuserService atBaseuserService;

    /**
     * 添加前端用户注册表
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加前端用户注册表", response = ApiResult.class)
    public ApiResult<Boolean> addAtBaseuser(AtBaseuser atBaseuser) throws Exception {
        if (LoginUtil.getUserId() == null) {
            return ApiResult.fail(ApiCode.UNAUTHORIZED);
        }
        atBaseuser.setCreatetitle(LoginUtil.getUserId().intValue());
        boolean flag = atBaseuserService.saveAtBaseuser(atBaseuser);
        return ApiResult.result(flag);
    }

    /**
     * 修改前端用户注册表
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改前端用户注册表", response = ApiResult.class)
    public ApiResult<Boolean> updateAtBaseuser(AtBaseuser atBaseuser) throws Exception {
        boolean flag = atBaseuserService.updateAtBaseuser(atBaseuser);
        return ApiResult.result(flag);
    }

    /**
     * 删除前端用户注册表
     */
    @RequestMapping(value = "delete", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除前端用户注册表", response = ApiResult.class)
    public ApiResult<Boolean> deleteAtBaseuser(String ids) throws Exception {
        String idArr[] = ids.split(",");
        for (String id : idArr) {
            atBaseuserService.deleteAtBaseuser(Long.parseLong(id));
        }

        return ApiResult.ok();
    }

    /**
     * 获取前端用户注册表详情
     */
    @RequestMapping(value = "info", produces = "application/json; charset=utf-8")
    @ApiOperation(value = "前端用户注册表详情", response = AtBaseuser.class)
    public ApiResult<AtBaseuser> getAtBaseuser(Long id) throws Exception {
        AtBaseuser atBaseuser = atBaseuserService.getById(id);
        return ApiResult.ok(atBaseuser);
    }

    /**
     * 分页获取前端用户列表
     */
    @GetMapping("/getPageList")
    @ApiOperation(value = "分页获取前端用户列表", response = AtBaseuserQueryVo.class)
    public ApiResult<Paging<AtBaseuserQueryVo>> getClientUserPageList(AtBaseuserPageParam pageParam) throws Exception {
        return ApiResult.ok(atBaseuserService.getAtBaseuserPageList(pageParam));
    }

    /**
     * @param id 用户ID
     * @return 审核用户
     * @throws Exception 审核异常
     */
    @PostMapping("/check")
    @ApiOperation(value = "分页获取前端用户列表", response = AtBaseuserQueryVo.class)
    public ApiResult<Paging<AtBaseuserQueryVo>> checkUser(Long id) throws Exception {
        return ApiResult.result(atBaseuserService.checkUser(id));
    }

    @GetMapping("/search")
    @ApiModelProperty(value = "根据电话搜索用户")
    public ApiResult<List<AtBaseuserQueryVo>> searchUser(String keyword) {
        return ApiResult.ok(atBaseuserService.searchUser(keyword));
    }
}

